package me.nibo.bignerdranch.hellomoon;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * @author nibo
 */
public class HelloMoonFragment extends Fragment {
    private static final String TAG = HelloMoonFragment.class.getSimpleName();
    private AudioPlayer mPlayer;
    private Button mPlayButton;
    private Button mStopButton;
    private Button mPauseButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_hello_moon, container, false);
        mPlayer = new AudioPlayer();
        mPlayButton = (Button) v.findViewById(R.id.hellomoon_play);
        mPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.start(getActivity());
            }
        });
        mStopButton = (Button) v.findViewById(R.id.hellomoon_stop);
        mStopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.stop();
            }
        });
        mPauseButton = (Button) v.findViewById(R.id.hellomoon_pause);
        mPauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.pause();
            }
        });
        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPlayer.stop();
        Log.d(TAG, "停止播放");
    }
}
